﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Roslyn.Scripting;
using Roslyn.Scripting.CSharp;
using System.Reflection;
using System.Windows;

namespace AlgorithmBox.Script
{
	public class AlgorithmExecuter
	{
		public static bool IsOn = false;

		ScriptEngine engine;
		Session session;

		public AlgorithmExecuter()
		{
			engine = new ScriptEngine(
				new Assembly[]
				{
					typeof(System.Windows.Window).Assembly,
					typeof(Console).Assembly
				},
				new string[]
				{
					"System",
					"System.Windows"
				}
			);
			session = Session.Create(this);
		}

		public object Run(string statement)
		{
			try
			{
				return engine.Execute<object>(statement, session);
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message);
				return null;
			}
		}

		public void NewSession()
		{
			session = Session.Create();
		}
	}
}
